package com.itheima.service.role.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.role.RoleMapper;
import com.itheima.service.role.RoleService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class RoleServiceImpl implements RoleService {
    /**
     * 业务层处理数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Role> list(String pageNum, String pageSize) {

        SqlSession session = null;
        try {
            //获取session对象
            session = MapperFactory.getSqlSession();
            //调用getMapper方法
            RoleMapper mapper = session.getMapper(RoleMapper.class);
            //设置初始页面显示数据 1页 显示10条数据
            int pageN = 1;
            int pageS = 5;
            //对页面数据进行非空判断然后赋值
            if (pageNum != null) {
                pageN = Integer.parseInt(pageNum);
            }
            if (pageSize != null) {
                pageS = Integer.parseInt(pageSize);
            }
            PageHelper.startPage(pageN, pageS);
            //查询所有数据
            List<Role> list = mapper.list();
            //返回list结果
            return list;
        } catch (Exception e) {
            TransactionUtil.rollback(session);
            e.printStackTrace();
            throw new RuntimeException("查询失败");
        } finally {
            //释放资源
            TransactionUtil.close(session);
        }
    }

    /**
     * 新增
     *
     * @param role
     */
    @Override
    public void save(Role role) {
        SqlSession session = null;
        try {
            //获取session对象
            session = MapperFactory.getSqlSession();
            //调用getMapper方法
            RoleMapper mapper = session.getMapper(RoleMapper.class);
            //生成UUID
            role.setId(UUID.randomUUID().toString().replaceAll("-", ""));

            mapper.save(role);
            //提交事务
            TransactionUtil.commit(session);
        } catch (Exception e) {
            TransactionUtil.rollback(session);
            e.printStackTrace();
        }
        //释放资源
        TransactionUtil.close(session);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deleteByIds(String ids) {
        SqlSession session = null;
        //设置标识flag为false
        boolean flag = false;
        try {
            //讲ids数组进行分割
            String[] id = ids.split(",");
            System.out.println(id);
            //获取session对象
            session = MapperFactory.getSqlSession();
            RoleMapper mapper = session.getMapper(RoleMapper.class);
            //调用持久层方法
            mapper.delete(id);
            //提交事务
            TransactionUtil.commit(session);
            //设置flag
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(session);
        }
        //返回flag
        return flag;
    }

    /**
     * 通过Id查询role表
     *
     * @param id
     * @return
     */
    @Override
    public List<String> findById(String id) {
        List<String> pids = new ArrayList<String>();
        //获取session对象
        SqlSession session = MapperFactory.getSqlSession();
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        //调用findById方法查询id对应的权限名
        List<Role> role = mapper.findById(id);
        if(!role.isEmpty()){
            List<Permission> permissions = role.get(0).getPermissions();
            //遍历集合
            for (Permission permission : permissions) {
                //把获取到的权限名添加到role_permission中
                pids.add(permission.getId());
            }

            TransactionUtil.close(session);
        }
        //返回role对象
        return pids;
    }

    /**
     *删除角色的权限
     * @param roleId
     */
    @Override
    public void deletePermission(String roleId) {
        SqlSession session = null;
        try {
            //获取session对象
            session = MapperFactory.getSqlSession();
            RoleMapper mapper = session.getMapper(RoleMapper.class);
            //调用持久层方法删除roleId对应的权限名
            mapper.deletePermission(roleId);
            //提交事务
            TransactionUtil.commit(session);
        } catch (Exception e) {
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(session);
        }
    }

    /**
     * 给角色设置权限
     * @param pids
     * @param roleId
     */
    @Override
    public void savePermission(String[] pids, String roleId) {
        SqlSession session = null;
        try {
            //获取session对象
            session = MapperFactory.getSqlSession();
            RoleMapper mapper = session.getMapper(RoleMapper.class);
            //调用持久层方法
            for (String pid : pids) {
                mapper.savePermission(pid, roleId);
            }
            //提交事务
            TransactionUtil.commit(session);
        } catch (Exception e) {
            TransactionUtil.rollback(session);
            e.printStackTrace();
        } finally {
            //释放资源
            TransactionUtil.close(session);
        }
    }
}
